package com.zfsn.okaria
import android.content.Context
import android.os.Parcelable
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.GsonUtils
import com.elvishew.xlog.XLog
import java.io.Serializable

class ARouteUtils private constructor() : Serializable {
    //private constructor()构造器私有化
    companion object {
        const val route_key_fragment = "route_key_fragment"
        const val route_intercept = "/app/a/intercept"//拦截
        const val route_single_task = "/app/f/single/task"//

        @JvmStatic
        fun getInstance(): ARouteUtils { //全局访问点
            return SingletonHolder.mInstance
        }
    }
    private fun convertRoute(url: String, params: Map<String, Any?>?=null,context: Context? = null){
        val build = ARouter.getInstance().build(url)
        XLog.i("ARoute:::::::convertRoute::::: url:$url "+":::"+ GsonUtils.toJson(params))
        params?.let {
            it.forEach { (t, u) ->
                if(u!=null){
                    when(u){
                        is String->{
                            build.withString(t, u)
                        }
                        is Int->{
                            build.withInt(t, u)
                        }
                        is Long->{
                            build.withLong(t, u)
                        }
                        is Double->{
                            build.withDouble(t, u)
                        }
                        is Float->{
                            build.withFloat(t, u)
                        }
                        is CharSequence->{
                            build.withCharSequence(t, u)
                        }
                        is Char->{
                            build.withChar(t, u)
                        }
                        is Boolean->{
                            build.withBoolean(t, u)
                        }
                        is Serializable->{
                            build.withSerializable(t, u)
                        }
                        is Parcelable ->{
                            build.withParcelable(t, u)
                        }
                        else->{
                            build.withString(t, GsonUtils.toJson(u))
                        }
                    }
                }
            }
        }
        build.navigation(context)
    }

    /**
     * 判断一个类是JAVA类型还是用户定义类型
     * @param clz
     * @return
     */
    fun isJavaClass(clz: Class<*>?): Boolean {
        return clz != null && clz.classLoader == null
    }

    fun openAny(path:String?,params: Map<String, Any?>?=null){
        XLog.i("ARoute:::::openAny::::::: $path "+":::"+ GsonUtils.toJson(params))
        if(path?.isNotEmpty() == true){
           if(path.contains("/f/")){
               openIntercept(path,params)
           }else{
               convertRoute(path, params)
           }
        }
    }
    fun openIntercept(routePath:String,params: Map<String, Any?>?=null){
        val resultParams =  mutableMapOf<String,Any?>(route_key_fragment to routePath)
        resultParams.putAll(params?: mapOf())
        XLog.i("ARoute:::::::openIntercept::::: $routePath "+":::"+ GsonUtils.toJson(resultParams))
        convertRoute(route_intercept, resultParams)
    }
    private object SingletonHolder {
        //静态内部类
        val mInstance: ARouteUtils = ARouteUtils()
    }
    private fun readResolve(): Any { //防止单例对象在反序列化时重新生成对象
        return SingletonHolder.mInstance
    }
}